﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

using ToyundaPhone.Database;

namespace DatabaseTest
{
	public partial class MainPage : PhoneApplicationPage
	{
		// Constructeur
		public MainPage()
		{
			InitializeComponent();
		}

		private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
		{
			
		}

		private void CreateButton_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			SongDatabase db = (Application.Current as App).SongDatabase;
			output.Items.Add("Inserting 2 songs.");
			Song[] songs = new Song[] {
				new Song() { Title = "Please, be my lover", Work = "A certain dumbest anime", Type = "JP" },
				new Song() { Title = "Someday, Somewhere", Work = "Mirai no mukou ni", Type = "JP", Favorite = true }
			};
			db.Songs.InsertAllOnSubmit(songs);
			output.Items.Add("Inserting songs in custom playlists");
			Playlist[] playlists = new Playlist[] {
				new Playlist() { Name = "First playlist" },
				new Playlist() { Name = "Second playlist" }
			};
			PlaylistSong[] playlistSongs = new PlaylistSong[] {
				new PlaylistSong() { SongSong = songs[0], PlaylistPlaylist = playlists[0]},
				new PlaylistSong() { SongSong = songs[0], PlaylistPlaylist = playlists[1]},
				new PlaylistSong() { SongSong = songs[1], PlaylistPlaylist = playlists[0]}
			};
			db.Playlists.InsertAllOnSubmit(playlists);
			db.PlaylistSongs.InsertAllOnSubmit(playlistSongs);
			output.Items.Add("Finalizing.");
			db.SubmitChanges();
		}

		private void UpdateButton_Click(object sender, RoutedEventArgs e)
		{
			SongDatabase db = (Application.Current as App).SongDatabase;
			output.Items.Add("Inserting 1 new song.");
			Song[] songs = new Song[] {
				new Song() { Title = "Love me, please", Work = "A romance of someday's dreamers", Type = "EN" }
			};
			db.Songs.InsertAllOnSubmit(songs);
			output.Items.Add("Inserting the new song in second playlist");
			Playlist[] playlists = new Playlist[] {
				(from pl in db.Playlists where pl.Name == "Second playlist" select pl).First()
			};
			PlaylistSong[] playlistSongs = new PlaylistSong[] {
				new PlaylistSong() { SongSong = songs[0], PlaylistPlaylist = playlists[0]},
			};
			db.PlaylistSongs.InsertAllOnSubmit(playlistSongs);
			output.Items.Add("Finalizing.");
			db.SubmitChanges();
		}

		private void DeleteButton_Click(object sender, RoutedEventArgs e)
		{
			SongDatabase db = (Application.Current as App).SongDatabase;
			output.Items.Add("Renaming a song.");
			Song[] songs = new Song[] {
				(from s in db.Songs where s.Id == 1 select s).First(),
				(from s in db.Songs where s.Id == 2 select s).First()
			};
			songs[0].Title = "You are my lover";
			db.Songs.DeleteOnSubmit(songs[1]);
			output.Items.Add("Finalizing.");
			db.SubmitChanges();
		}
	}
}